/************************************************************************************/
/*  Program name CAPEXT_ASSIGNMENT.sql                                              */
/*  Creater:  WONG Wing Kin (PCCW - UniHub)                                         */
/*************   History   *************************                                */
/* 30 NOV, 2005   WONG Wing Kin         First created                               */
/************************************************************************************/
ALTER SESSION SET NLS_DATE_FORMAT='YYYYMMDD HH24:MI:SS';
SET SERVEROUTPUT ON SIZE 1000000;
SET VERIFY OFF

DECLARE
	lv_Prog_ID		VARCHAR2(100)	:=	'CAPEXT_ASSIGNMENT';

	lv_Scc_Count	NUMBER	:=	0;
	lv_Err_Count	NUMBER	:=	0;
	lv_Err_Msg		VARCHAR2(500)	:=	'No Error';

	lv_FileName		VARCHAR2(100)	:=	'CAPFND_ASSIGNMENT.dat';
	lv_PathName		VARCHAR2(100)	:=	'/tmp';
	lv_UTL_File		UTL_FILE.File_Type;

	lv_NGO			VARCHAR2(100);
	lv_NGO_Count	NUMBER;

	CURSOR Cur_F(p_NGO VARCHAR2) IS
	SELECT
		A.*,
		J.Name				ASS_Job_Name,
		S.User_Status		ASS_User_Status,
		L.Location_Code		ASS_Location_Code,
		P1.Employee_Number	ASS_Employee_Number,
		P2.Employee_Number	ASS_Sup_Emp_Number,
		O.Name				ASS_Org_Name
	FROM
		PER_BUSINESS_GROUPS			B,
		PER_ALL_ASSIGNMENTS_F		A,
		PER_JOBS					J,
		PER_ASSIGNMENT_STATUS_TYPES	S,
		HR_LOCATIONS				L,
		PER_ALL_PEOPLE_F			P1,
		PER_ALL_PEOPLE_F			P2,
		HR_ORGANIZATION_UNITS		O
	WHERE	SUBSTR(B.Name,4,4)			=	p_NGO
	AND		A.Business_Group_ID			=	B.Business_Group_ID
	AND		A.Job_ID					=	J.Job_ID(+)
	AND		A.Assignment_Status_Type_ID	=	S.Assignment_Status_Type_ID(+)
	AND		A.Location_ID				=	L.Location_ID(+)
	AND		A.Person_ID					=	P1.Person_ID
	AND		A.Supervisor_ID				=	P2.Person_ID(+)
	AND		A.Organization_ID			=	O.Organization_ID(+)
	AND		SYSDATE		BETWEEN		A.Effective_Start_Date
						AND			A.Effective_End_Date
	AND		SYSDATE		BETWEEN		P1.Effective_Start_Date
						AND			P1.Effective_End_Date
	AND		SYSDATE		BETWEEN		P2.Effective_Start_Date(+)
						AND			P2.Effective_End_Date(+)
	ORDER BY	A.Person_ID;

BEGIN
	lv_NGO			:=	UPPER('&1');

	SELECT	COUNT('A')
	INTO	lv_NGO_Count
	FROM	HR_ALL_ORGANIZATION_UNITS
	WHERE	SUBSTR(Name,4,4)	=	lv_NGO;

	IF	lv_NGO_Count	>=	1	THEN
		lv_Scc_Count	:=	0;
		lv_Err_Msg		:=	'No Error';
		lv_UTL_File		:=	UTL_FILE.FOpen(lv_PathName, lv_FileName, 'w');
		FOR C1 IN Cur_F(lv_NGO) LOOP

			IF	C1.Job_ID						IS	NOT NULL	AND
				C1.ASS_Job_Name					IS	NULL		THEN
				DBMS_OUTPUT.PUT_LINE(lv_Prog_ID||' Job ['||C1.Job_ID||'] Not Found.');
			END IF;

			IF	C1.Assignment_Status_Type_ID	IS	NOT NULL	AND
				C1.ASS_User_Status				IS	NULL		THEN
				DBMS_OUTPUT.PUT_LINE(lv_Prog_ID||' Asg Status Type ['||C1.Assignment_Status_Type_ID||'] Not Found.');
			END IF;

			IF	C1.Location_ID					IS	NOT NULL	AND
				C1.ASS_Location_Code			IS	NULL		THEN
				DBMS_OUTPUT.PUT_LINE(lv_Prog_ID||' Location ['||C1.Location_ID||'] Not Found.');
			END IF;

			IF	C1.Supervisor_ID				IS	NOT NULL	AND
				C1.ASS_Sup_Emp_Number			IS	NULL		THEN
				DBMS_OUTPUT.PUT_LINE(lv_Prog_ID||' Supervisor ['||C1.Supervisor_ID||'] Not Found.');
			END IF;

			IF	C1.Person_ID					IS	NOT NULL	AND
				C1.ASS_Employee_Number			IS	NULL		THEN
				DBMS_OUTPUT.PUT_LINE(lv_Prog_ID||' Person ['||C1.Person_ID||'] Not Found.');
			END IF;

			IF	C1.Organization_ID				IS	NOT NULL	AND
				C1.ASS_Org_Name					IS	NULL		THEN
				DBMS_OUTPUT.PUT_LINE(lv_Prog_ID||' Organization ['||C1.Organization_ID||'] Not Found.');
			END IF;

			UTL_FILE.PUT_LINE(
				lv_UTL_File,
				C1.Assignment_ID||CHR(9)||
				C1.Effective_Start_Date||CHR(9)||
				C1.Effective_End_Date||CHR(9)||
					C1.Business_Group_ID||CHR(9)||
				C1.Recruiter_ID||CHR(9)||
				C1.Grade_ID||CHR(9)||
				C1.Position_ID||CHR(9)||
					C1.Job_ID||CHR(9)||
					C1.Assignment_Status_Type_ID||CHR(9)||
				C1.Payroll_ID||CHR(9)||
					C1.Location_ID||CHR(9)||
				C1.Person_Referred_By_ID||CHR(9)||
					C1.Supervisor_ID||CHR(9)||
				C1.Special_Ceiling_Step_ID||CHR(9)||
					C1.Person_ID||CHR(9)||
				C1.Recruitment_Activity_ID||CHR(9)||
				C1.Source_Organization_ID||CHR(9)||
					C1.Organization_ID||CHR(9)||
				C1.People_Group_ID||CHR(9)||
				C1.Soft_Coding_Keyflex_ID||CHR(9)||
				C1.Vacancy_ID||CHR(9)||
				C1.Pay_Basis_ID||CHR(9)||
				C1.Assignment_Sequence||CHR(9)||
				C1.Assignment_Type||CHR(9)||
				C1.Primary_Flag||CHR(9)||
				C1.Application_ID||CHR(9)||
				C1.Assignment_Number||CHR(9)||
				C1.Change_Reason||CHR(9)||
				C1.Comment_ID||CHR(9)||
				C1.Date_Probation_End||CHR(9)||
				C1.Default_Code_Comb_ID||CHR(9)||
				C1.Employment_Category||CHR(9)||
				C1.Frequency||CHR(9)||
				C1.Internal_Address_Line||CHR(9)||
				C1.Manager_Flag||CHR(9)||
				C1.Normal_Hours||CHR(9)||
				C1.Perf_Review_Period||CHR(9)||
				C1.Perf_Review_Period_Frequency||CHR(9)||
				C1.Period_Of_Service_ID||CHR(9)||
				C1.Probation_Period||CHR(9)||
				C1.Probation_Unit||CHR(9)||
				C1.Sal_Review_Period||CHR(9)||
				C1.Sal_Review_Period_Frequency||CHR(9)||
				C1.Set_Of_Books_ID||CHR(9)||
				C1.Source_Type||CHR(9)||
				C1.Time_Normal_Finish||CHR(9)||
				C1.Time_Normal_Start||CHR(9)||
				C1.Bargaining_Unit_Code||CHR(9)||
				C1.Labour_Union_Member_Flag||CHR(9)||
				C1.Hourly_Salaried_Code||CHR(9)||
				C1.Contract_ID||CHR(9)||
				C1.Collective_Agreement_ID||CHR(9)||
				C1.Cagr_ID_Flex_Num||CHR(9)||
				C1.Cagr_Grade_Def_ID||CHR(9)||
				C1.Establishment_ID||CHR(9)||
				C1.Request_ID||CHR(9)||
				C1.Program_Application_ID||CHR(9)||
				C1.Program_ID||CHR(9)||
				C1.Program_Update_Date||CHR(9)||
				C1.Ass_Attribute_Category||CHR(9)||
				C1.Ass_Attribute1||CHR(9)||
				C1.Ass_Attribute2||CHR(9)||
				C1.Ass_Attribute3||CHR(9)||
				C1.Ass_Attribute4||CHR(9)||
				C1.Ass_Attribute5||CHR(9)||
				C1.Ass_Attribute6||CHR(9)||
				C1.Ass_Attribute7||CHR(9)||
				C1.Ass_Attribute8||CHR(9)||
				C1.Ass_Attribute9||CHR(9)||
				C1.Ass_Attribute10||CHR(9)||
				C1.Ass_Attribute11||CHR(9)||
				C1.Ass_Attribute12||CHR(9)||
				C1.Ass_Attribute13||CHR(9)||
				C1.Ass_Attribute14||CHR(9)||
				C1.Ass_Attribute15||CHR(9)||
				C1.Ass_Attribute16||CHR(9)||
				C1.Ass_Attribute17||CHR(9)||
				C1.Ass_Attribute18||CHR(9)||
				C1.Ass_Attribute19||CHR(9)||
				C1.Ass_Attribute20||CHR(9)||
				C1.Ass_Attribute21||CHR(9)||
				C1.Ass_Attribute22||CHR(9)||
				C1.Ass_Attribute23||CHR(9)||
				C1.Ass_Attribute24||CHR(9)||
				C1.Ass_Attribute25||CHR(9)||
				C1.Ass_Attribute26||CHR(9)||
				C1.Ass_Attribute27||CHR(9)||
				C1.Ass_Attribute28||CHR(9)||
				C1.Ass_Attribute29||CHR(9)||
				C1.Ass_Attribute30||CHR(9)||
				C1.Last_Update_Date||CHR(9)||
					C1.Last_Updated_By||CHR(9)||
				C1.Last_Update_Login||CHR(9)||
					C1.Created_By||CHR(9)||
				C1.Creation_Date||CHR(9)||
				C1.Title||CHR(9)||
				C1.Object_Version_Number||CHR(9)||
				C1.Notice_Period||CHR(9)||
				C1.Notice_Period_UOM||CHR(9)||
				C1.Employee_Category||CHR(9)||
				C1.Work_At_Home||CHR(9)||
				C1.Job_Post_Source_Name||CHR(9)||
				C1.Posting_Content_ID||CHR(9)||
				C1.Period_Of_Placement_Date_Start||CHR(9)||
				C1.Vendor_ID||CHR(9)||
				C1.Vendor_Employee_Number||CHR(9)||
				C1.Vendor_Assignment_Number||CHR(9)||
				C1.Assignment_Category||CHR(9)||
				C1.Project_Title||CHR(9)||
				C1.Applicant_Rank||CHR(9)||
				CAPBREXT_COMMON.GetBGName(C1.Business_Group_ID)||CHR(9)||
				C1.ASS_Job_Name||CHR(9)||
				C1.ASS_User_Status||CHR(9)||
				C1.ASS_Location_Code||CHR(9)||
				C1.ASS_Sup_Emp_Number||CHR(9)||
				C1.ASS_Employee_Number||CHR(9)||
				C1.ASS_Org_Name||CHR(9)||
				CAPBREXT_COMMON.GetUser(C1.Last_Updated_By)||CHR(9)||
				CAPBREXT_COMMON.GetUser(C1.Created_By)
			);
			lv_Scc_Count	:=	lv_Scc_Count	+	1;
		END LOOP;
		IF	lv_Scc_Count	=	0	THEN
			lv_Err_Msg	:=	lv_NGO||': No Assignment records for this business group';
		END IF;
		UTL_FILE.FClose(lv_UTL_File);
		INSERT INTO CAPCNV_LOG(
			PROG_ID,
			PROCESS_DATE,
			RECORD_PROCESSED,
			RECORD_REJECTED,
			RECORD_EXTRACTED,
			ERR_MSG
		) VALUES (
			lv_Prog_ID,
			SYSDATE,
			lv_Scc_Count,
			lv_Err_Count,
			lv_Scc_Count-lv_Err_Count,
			lv_Err_Msg
		);
	ELSE
		lv_Err_Count	:=	lv_Err_Count + 1;
		lv_Err_Msg		:=	lv_NGO||': No such BG exists';
		INSERT INTO CAPCNV_LOG(
			PROG_ID,
			PROCESS_DATE,
			RECORD_PROCESSED,
			RECORD_REJECTED,
			RECORD_EXTRACTED,
			ERR_MSG
		) VALUES (
			lv_Prog_ID,
			SYSDATE,
			lv_Scc_Count,
			lv_Err_Count,
			lv_Scc_Count-lv_Err_Count,
			lv_Err_Msg
		);
	END IF;

	COMMIT;
END;
/